package com.selen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.selen.dto.DeptPageDTO;
import com.selen.dto.PageDTO;
import com.selen.dto.DeptInsertDTO;
import com.selen.dto.DeptUpdateDTO;
import com.selen.entities.Dept;
import com.selen.exception.ServiceException;
import com.selen.mapper.DeptMapper;
import com.selen.mapper.EmpMapper;
import com.selen.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Selen
 */
@Service
@CacheConfig(cacheNames = "dept")
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(DeptInsertDTO dto) {
        Dept dept = BeanUtil.copyProperties(dto, Dept.class);
        // 设置默认值
        dept.setDeleted(0);
        dept.setCreated(LocalDateTime.now());
        dept.setUpdated(LocalDateTime.now());
        // DB添加
        int result = deptMapper.insert(dept);
        if (result <= 0) {
            throw new ServiceException("DB添加失败");
        }
        return result;
    }

    @Cacheable(key = "#id", condition = "#id != null ", unless = "#result == null ")
    @Override
    public Dept selectById(Long id) {
        Dept result = deptMapper.selectById(id);
        if (ObjectUtil.isNull(result)){
            throw new ServiceException("ID不存在");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int updateById(DeptUpdateDTO dto) {
        Dept dept = BeanUtil.copyProperties(dto, Dept.class);
        // 设置默认值
        dept.setCreated(null);
        dept.setUpdated(LocalDateTime.now());
        // DB修改
        int result = deptMapper.updateById(dept);
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        // todo
        if (empMapper.existByDeptId(id)){
            throw new ServiceException("部门内仍有员工，删除失败");
        }
        int result = deptMapper.logicDeleteById(id);
        if (result <= 0){
            throw new ServiceException("DB 逻辑删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        // todo
        if (empMapper.existByDeptIds(ids)){
            throw new ServiceException("部门内仍有员工，删除失败");
        }
        int result = deptMapper.logicDeleteByIds(ids);
        if (result <= 0){
            throw new ServiceException("DB 批量逻辑删除失败");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null ")
    @Override
    public List<Dept> selectAll() {
        return deptMapper.selectAll();
    }

    @Cacheable(key = "#root.methodName + ':' + #dto.pageNum + ':' + #dto.pageSize ",
            unless = "#result == null ")
    @Override
    public PageInfo<Dept> page(PageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(deptMapper.selectAll());
    }

    @Cacheable(key = "#root.methodName + ':' + " +
            "#dto.getKeyword() + ':' + " +
            "#dto.getPageNum() + ':' + " +
            "#dto.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<Dept> dimSelect(DeptPageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(deptMapper.selectByDimName(dto.getKeyword()));
    }
}
